package com.example.tables.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.example.tables.bean.TableABean;
import com.example.tables.bean.TableBBean;
import com.example.tables.bean.TableBReadListener;
import com.example.tables.bean.TableReadListener;
import com.example.tables.service.TableBService;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tableb")
public class TableBController {

    @Autowired
    TableBService tableBService;

    @RequestMapping("/addData")
    public Object addData(@RequestParam(value = "a")String a,
                          @RequestParam(value = "b")String b,
                          @RequestParam(value = "c")String c,
                          @RequestParam(value = "d")String d,
                          @RequestParam(value = "e")String e,
                          @RequestParam(value = "f")String f,
                          @RequestParam(value = "aa")int aa,
                          @RequestParam(value = "bb")int bb,
                          @RequestParam(value = "cc")int cc,
                          @RequestParam(value = "dd")int dd,
                          @RequestParam(value = "ee")int ee,
                          @RequestParam(value = "aaRight")int aaRight,
                          @RequestParam(value = "bbRight")int bbRight,
                          @RequestParam(value = "ccRight")int ccRight,
                          @RequestParam(value = "ddRight")int ddRight,
                          @RequestParam(value = "eeRight")int eeRight
                          ){
        Map<String,Object> map =new HashMap<>();
        TableBBean tableBBean = new TableBBean();
        tableBBean.setA(a);
        tableBBean.setB(b);
        tableBBean.setC(c);
        tableBBean.setD(d);
        tableBBean.setE(e);
        tableBBean.setF(f);
        tableBBean.setAa(aa);
        tableBBean.setBb(bb);
        tableBBean.setCc(cc);
        tableBBean.setDd(dd);
        tableBBean.setEe(ee);
        tableBBean.setAaRight(aaRight);
        tableBBean.setBbRight(bbRight);
        tableBBean.setCcRight(ccRight);
        tableBBean.setDdRight(ddRight);
        tableBBean.setEeRight(eeRight);

        int check =tableBService.addData(tableBBean);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }

        return map;
    }


    @RequestMapping("/update")
    public Object update(@RequestParam(value = "a")String a,
                         @RequestParam(value = "b")String b,
                         @RequestParam(value = "c")String c,
                         @RequestParam(value = "d")String d,
                         @RequestParam(value = "e")String e,
                         @RequestParam(value = "f")String f,
                         @RequestParam(value = "aa")int aa,
                         @RequestParam(value = "bb")int bb,
                         @RequestParam(value = "cc")int cc,
                         @RequestParam(value = "dd")int dd,
                         @RequestParam(value = "ee")int ee,
                         @Param("aaRight")int aaRight,
                         @Param("bbRight")int bbRight,
                         @Param("ccRight")int ccRight,
                         @Param("ddRight")int ddRight,
                         @Param("eeRight")int eeRight){
        Map<String,Object> map =new HashMap<>();

        int check =tableBService.update( a,  b,  c,  d,  e,  f,  aa,  bb,  cc,  dd,  ee, aaRight, bbRight, ccRight, ddRight, eeRight);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }
        return map;
    }


    @RequestMapping("/delete")
    public Object delete(@RequestParam(value = "a")String a){
        Map<String,Object> map =new HashMap<>();
        int check =tableBService.delete(a);
        if(check==1){
            map.put("code",200);
            map.put("msg", "success!");
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
        }
        return map;

    }

    @RequestMapping("/deleteC")
    public Object deleteC(@RequestParam(value = "c")String c){
        Map<String,Object> map =new HashMap<>();
        int check =tableBService.deleteC(c);
        if(check!=0){
            map.put("code",200);
            map.put("msg", "success!");
            map.put("numberofdata", check);
        }else{
            map.put("code",100);
            map.put("msg", "fail!");
            map.put("numberofdata", check);
        }
        return map;
    }

    @RequestMapping("/import")
    public void importExcel() throws FileNotFoundException {
        File file = new File("C:/tableb.xlsx");
        InputStream is = new FileInputStream(file);

        TableBReadListener listener = new TableBReadListener(tableBService);

        EasyExcel.read(is,
                        TableBBean.class,
                        listener).sheet(0)
                .headRowNumber(1)
                .doRead();
    }

    @RequestMapping("/getall")
    public List<TableBBean> getAll(){
        List<TableBBean> data  = tableBService.getAll();

        return data;
    }

    @RequestMapping("/export")
    public void export(HttpServletResponse response)throws IOException {
        List<TableBBean> list = getAll();

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        response.setCharacterEncoding("utf-8");

        String fileName = URLEncoder.encode("tableb","utf-8").replaceAll("\\+","%20");
        response.setHeader("Content-disposition", "attachment;filename="+fileName+".xlsx");

        EasyExcel.write(response.getOutputStream())
                .head(TableBBean.class)
                .excelType(ExcelTypeEnum.XLSX)
                .sheet(1)
                .doWrite(list);
    }



}
